package com.tianhui.consignortwofei.mvp.ui.activity.deliverGoods.inquiry;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.entity.region.RegionInfo;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.widget.itemView.CitySearchView;
import com.fgs.common.widget.itemView.InputItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.SourcePathModel;
import com.tianhui.consignortwofei.mvp.model.enty.SourcePathInfo;
import com.tianhui.consignortwofei.mvp.ui.activity.SourcePathListActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/8/31
 * @description
 **/
public class InquiryDeliverGoodsStepOneActivity extends InquiryDeliverGoodsActivity {

    private static final int REQUEST_CODE_HISTORY = 1001;

    @BindView(R.id.activity_inquiry_deliver_goods_step_one_placeSourcePathLinearLayout)
    LinearLayout mPlaceSourcePathLinearLayout;
    @BindView(R.id.activity_inquiry_deliver_goods_step_one_deliverSourcePathLinearLayout)
    LinearLayout mDeliverSourcePathLinearLayout;
    @BindView(R.id.activity_inquiry_deliver_goods_step_one_destinationSourcePathLinearLayout)
    LinearLayout mDestinationSourcePathLinearLayout;

//    protected ClickItemView mPlaceClickItemView;
//    protected ClickItemView mDeliverClickItemView;
//    protected ClickItemView mDestinationClickItemView;

    protected CitySearchView mPlaceClickItemView;
    protected CitySearchView mDeliverClickItemView;
    protected CitySearchView mDestinationClickItemView;

    private TextView mPlaceHistoryTextView;
    private TextView mDeliverHistoryTextView;
    private TextView mDestinationHistoryTextView;

    private TextView mPlaceSaveTextView;
    private TextView mDeliverSaveTextView;
    private TextView mDestinationSaveTextView;

    protected InputItemView mPlaceUserInputItemView;
    protected InputItemView mDeliverUserInputItemView;
    protected InputItemView mDestinationUserInputItemView;

    protected InputItemView mPlaceAddressInputItemView;
    protected InputItemView mDeliverAddressInputItemView;
    protected InputItemView mDestinationAddressInputItemView;


    protected InputItemView mPlacePhoneInputItemView;
    protected InputItemView mDeliverPhoneInputItemView;
    protected InputItemView mDestinationPhoneInputItemView;

    private SourcePathModel mSourcePathModel;

    @Override
    public int getLayoutId() {
        return R.layout.activity_inquiry_deliver_goods_step_one;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public void initData() {
        mSourcePathModel = new SourcePathModel();
    }

    @Override
    public void initView() {
        mPlaceClickItemView = mPlaceSourcePathLinearLayout.findViewById(R.id.layout_deliver_source_path_sourcePathCitySearchView);
        mDeliverClickItemView = mDeliverSourcePathLinearLayout.findViewById(R.id.layout_deliver_source_path_sourcePathCitySearchView);
        mDestinationClickItemView = mDestinationSourcePathLinearLayout.findViewById(R.id.layout_deliver_source_path_sourcePathCitySearchView);


        mPlaceHistoryTextView = mPlaceSourcePathLinearLayout.findViewById(R.id.layout_deliver_source_path_historyTextView);
        mDeliverHistoryTextView = mDeliverSourcePathLinearLayout.findViewById(R.id.layout_deliver_source_path_historyTextView);
        mDestinationHistoryTextView = mDestinationSourcePathLinearLayout.findViewById(R.id.layout_deliver_source_path_historyTextView);

        mPlaceSaveTextView = mPlaceSourcePathLinearLayout.findViewById(R.id.layout_deliver_source_path_saveTextView);
        mDeliverSaveTextView = mDeliverSourcePathLinearLayout.findViewById(R.id.layout_deliver_source_path_saveTextView);
        mDestinationSaveTextView = mDestinationSourcePathLinearLayout.findViewById(R.id.layout_deliver_source_path_saveTextView);

        mPlaceUserInputItemView = mPlaceSourcePathLinearLayout.findViewById(R.id.layout_deliver_source_path_userInputItemView);
        mDeliverUserInputItemView = mDeliverSourcePathLinearLayout.findViewById(R.id.layout_deliver_source_path_userInputItemView);
        mDestinationUserInputItemView = mDestinationSourcePathLinearLayout.findViewById(R.id.layout_deliver_source_path_userInputItemView);


        mPlaceAddressInputItemView = mPlaceSourcePathLinearLayout.findViewById(R.id.layout_deliver_source_path_addressInputItemView);
        mDeliverAddressInputItemView = mDeliverSourcePathLinearLayout.findViewById(R.id.layout_deliver_source_path_addressInputItemView);
        mDestinationAddressInputItemView = mDestinationSourcePathLinearLayout.findViewById(R.id.layout_deliver_source_path_addressInputItemView);

        mPlacePhoneInputItemView = mPlaceSourcePathLinearLayout.findViewById(R.id.layout_deliver_source_path_phoneInputItemView);
        mDeliverPhoneInputItemView = mDeliverSourcePathLinearLayout.findViewById(R.id.layout_deliver_source_path_phoneInputItemView);
        mDestinationPhoneInputItemView = mDestinationSourcePathLinearLayout.findViewById(R.id.layout_deliver_source_path_phoneInputItemView);

        mPlaceClickItemView.setTitle("取单地");
        mPlaceClickItemView.setContentHint("请选择取单地");
        mDeliverClickItemView.setTitle("装货地");
        mDeliverClickItemView.setContentHint("请选择装货地");
        mDestinationClickItemView.setTitle("目的地");
        mDestinationClickItemView.setContentHint("请选择目的地");

        mPlaceUserInputItemView.setTitle("取单人");
        mDeliverUserInputItemView.setTitle("装货人");
        mDestinationUserInputItemView.setTitle("收货人");


        mPlaceUserInputItemView.setShowTip(false);
        mPlacePhoneInputItemView.setShowTip(false);

        mDeliverUserInputItemView.setShowTip(false);
        mDeliverPhoneInputItemView.setShowTip(false);

        mDestinationUserInputItemView.setShowTip(false);
        mDestinationPhoneInputItemView.setShowTip(false);
    }

    @Override
    public void initListener() {
        mPlaceClickItemView.setOnCitySearchListener(new CitySearchView.OnCitySearchListener() {
            @Override
            public void citySearchCallback(RegionInfo regionInfo) {
                SourcePathInfo sourcePathInfo = new SourcePathInfo();
                sourcePathInfo.placeprovince = regionInfo.province;
                sourcePathInfo.placecity = regionInfo.city;
                sourcePathInfo.placecounty = regionInfo.county;
                sourcePathInfo.placecitycode = regionInfo.code;
                mGoodsInfo.placeParentCode = regionInfo.parentcode;
                setSelectPathInfo(KeyValue.RESULT_PLACE, sourcePathInfo);
            }
        });
        mDeliverClickItemView.setOnCitySearchListener(new CitySearchView.OnCitySearchListener() {
            @Override
            public void citySearchCallback(RegionInfo regionInfo) {
                SourcePathInfo sourcePathInfo = new SourcePathInfo();
                sourcePathInfo.placeprovince = regionInfo.province;
                sourcePathInfo.placecity = regionInfo.city;
                sourcePathInfo.placecounty = regionInfo.county;
                sourcePathInfo.placecitycode = regionInfo.code;
                mGoodsInfo.deliverParentCode = regionInfo.parentcode;
                setSelectPathInfo(KeyValue.RESULT_DELIVER, sourcePathInfo);

            }
        });

        mDeliverClickItemView.setOnCitySearchSelectListener(new CitySearchView.OnCitySearchSelectListener() {
            @Override
            public void onCitySelect(RegionInfo regionInfo) {
                SourcePathInfo sourcePathInfo = new SourcePathInfo();
                sourcePathInfo.placeprovince = regionInfo.province;
                sourcePathInfo.placecity = regionInfo.city;
                sourcePathInfo.placecounty = regionInfo.county;
                sourcePathInfo.placecitycode = regionInfo.code;
                mGoodsInfo.deliverParentCode = regionInfo.parentcode;
                setSelectPathInfo(KeyValue.RESULT_DELIVER, sourcePathInfo);
            }
        });

        mDestinationClickItemView.setOnCitySearchListener(new CitySearchView.OnCitySearchListener() {
            @Override
            public void citySearchCallback(RegionInfo regionInfo) {
                SourcePathInfo sourcePathInfo = new SourcePathInfo();
                sourcePathInfo.placeprovince = regionInfo.province;
                sourcePathInfo.placecity = regionInfo.city;
                sourcePathInfo.placecounty = regionInfo.county;
                sourcePathInfo.placecitycode = regionInfo.code;
                mGoodsInfo.destinationParentCode = regionInfo.parentcode;
                setSelectPathInfo(KeyValue.RESULT_DESTINATION, sourcePathInfo);
            }
        });

        mDestinationClickItemView.setOnCitySearchSelectListener(new CitySearchView.OnCitySearchSelectListener() {
            @Override
            public void onCitySelect(RegionInfo regionInfo) {
                SourcePathInfo sourcePathInfo = new SourcePathInfo();
                sourcePathInfo.placeprovince = regionInfo.province;
                sourcePathInfo.placecity = regionInfo.city;
                sourcePathInfo.placecounty = regionInfo.county;
                sourcePathInfo.placecitycode = regionInfo.code;
                mGoodsInfo.destinationParentCode = regionInfo.parentcode;
                setSelectPathInfo(KeyValue.RESULT_DESTINATION, sourcePathInfo);
            }
        });


        mPlaceHistoryTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Bundle bundle = new Bundle();
                bundle.putInt(KeyValue.KEY_TYPE, KeyValue.RESULT_PLACE);
                openActivityForResult(REQUEST_CODE_HISTORY, SourcePathListActivity.class, bundle);

            }
        });
        mDeliverHistoryTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt(KeyValue.KEY_TYPE, KeyValue.RESULT_DELIVER);
                openActivityForResult(REQUEST_CODE_HISTORY, SourcePathListActivity.class, bundle);
            }
        });
        mDestinationHistoryTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt(KeyValue.KEY_TYPE, KeyValue.RESULT_DESTINATION);
                openActivityForResult(REQUEST_CODE_HISTORY, SourcePathListActivity.class, bundle);
            }
        });

        mPlaceSaveTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String details = mPlaceAddressInputItemView.getContent();
                String placeTel = mPlacePhoneInputItemView.getContent();
                String placeUserName = mPlaceUserInputItemView.getContent();

                if (mGoodsInfo != null) {
                    if (TextUtils.isEmpty(mGoodsInfo.placeprovince)) {
                        showToast("请选择取单地");
                        return;
                    }
                    if (TextUtils.isEmpty(placeUserName)) {
                        showToast("请填写取单人");
                        return;
                    }
                    if (TextUtils.isEmpty(placeTel)) {
                        showToast("请填写取单人电话");
                        return;
                    }
                    Map<String, String> map = new HashMap<>();
                    map.put("placeprovince", mGoodsInfo.placeprovince);
                    map.put("placecity", mGoodsInfo.placecity);
                    map.put("placecounty", mGoodsInfo.placecounty);
                    map.put("placedetails", details);
                    map.put("placetel", placeTel);
                    map.put("placeusername", placeUserName);
                    map.put("type", "1");
                    map.put("placecitycode", mGoodsInfo.placecitycode);
                    requestSourcePathSave(map);
                }


            }
        });

        mDeliverSaveTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mGoodsInfo != null) {
                    String details = mDeliverAddressInputItemView.getContent();
                    String placeTel = mDeliverPhoneInputItemView.getContent();
                    String placeUserName = mDeliverUserInputItemView.getContent();
                    if (TextUtils.isEmpty(mGoodsInfo.placeprovince)) {
                        showToast("请选择装货地");
                        return;
                    }
                    if (TextUtils.isEmpty(placeUserName)) {
                        showToast("请填写装货人");
                        return;
                    }
                    if (TextUtils.isEmpty(placeTel)) {
                        showToast("请填写装货人电话");
                        return;
                    }

                    Map<String, String> map = new HashMap<>();
                    map.put("placeprovince", mGoodsInfo.deliverprovince);
                    map.put("placecity", mGoodsInfo.delivercity);
                    map.put("placecounty", mGoodsInfo.delivercounty);
                    map.put("placedetails", details);
                    map.put("placetel", placeTel);
                    map.put("placeusername", placeUserName);
                    map.put("type", "2");
                    map.put("placecitycode", mGoodsInfo.delivercitycode);
                    requestSourcePathSave(map);
                }
            }
        });

        mDestinationSaveTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mGoodsInfo != null) {
                    String details = mDestinationAddressInputItemView.getContent();
                    String placeTel = mDestinationPhoneInputItemView.getContent();
                    String placeUserName = mDestinationUserInputItemView.getContent();
                    if (TextUtils.isEmpty(mGoodsInfo.placeprovince)) {
                        showToast("请选择收货地");
                        return;
                    }
                    if (TextUtils.isEmpty(placeUserName)) {
                        showToast("请填写收货人");
                        return;
                    }
                    if (TextUtils.isEmpty(placeTel)) {
                        showToast("请填写收货人电话");
                        return;
                    }

                    Map<String, String> map = new HashMap<>();
                    map.put("placeprovince", mGoodsInfo.destinationprovince);
                    map.put("placecity", mGoodsInfo.destinationcity);
                    map.put("placecounty", mGoodsInfo.destinationcounty);
                    map.put("placedetails", details);
                    map.put("placetel", placeTel);
                    map.put("placeusername", placeUserName);
                    map.put("type", "3");
                    map.put("placecitycode", mGoodsInfo.destinationcitycode);
                    requestSourcePathSave(map);
                }
            }
        });
    }

    private void requestSourcePathSave(Map<String, String> map) {
        if (mSourcePathModel == null) return;
        mSourcePathModel.sourcePathSaveModel(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (!isFinishing() && !isDestroyed()) {
                    if (commonResponse.getCode() == 20000) {
                        showToast("保存成功");
                    } else {
                        showToast(commonResponse.getMsg());
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        SourcePathInfo pathInfo;
        if (requestCode == REQUEST_CODE && data != null) {
            pathInfo = data.getParcelableExtra(KeyValue.KEY_PATH_INFO);

            setSelectPathInfo(resultCode, pathInfo);
        } else if (requestCode == REQUEST_CODE_HISTORY && data != null) {
            pathInfo = data.getParcelableExtra(KeyValue.KEY_PATH_INFO);
            setSelectPathInfo(resultCode, pathInfo);
            if (resultCode == KeyValue.RESULT_PLACE) {
                setSelectPathHistory(mPlaceClickItemView, mPlaceAddressInputItemView, mPlaceUserInputItemView, mPlacePhoneInputItemView, pathInfo);
                mGoodsInfo.placecitycode = pathInfo.placecitycode;
                mGoodsInfo.placeParentCode = pathInfo.placecitycode;
            } else if (resultCode == KeyValue.RESULT_DELIVER) {
                setSelectPathHistory(mDeliverClickItemView, mDeliverAddressInputItemView, mDeliverUserInputItemView, mDeliverPhoneInputItemView, pathInfo);
                mGoodsInfo.delivercitycode = pathInfo.placecitycode;
                mGoodsInfo.deliverParentCode = pathInfo.placecitycode;
            } else if (resultCode == KeyValue.RESULT_DESTINATION) {
                setSelectPathHistory(mDestinationClickItemView, mDestinationAddressInputItemView, mDestinationUserInputItemView, mDestinationPhoneInputItemView, pathInfo);
                mGoodsInfo.destinationcitycode = pathInfo.placecitycode;
                mGoodsInfo.destinationParentCode = pathInfo.placecitycode;
            }

        }
    }

    private void setSelectPathInfo(int resultCode, SourcePathInfo pathInfo) {
        if (pathInfo == null) return;
        if (resultCode == KeyValue.RESULT_PLACE) {
            mGoodsInfo.placeprovince = pathInfo.placeprovince;
            mGoodsInfo.placecity = pathInfo.placecity;
            mGoodsInfo.placecounty = pathInfo.placecounty;
            mGoodsInfo.placecitycode = pathInfo.placecitycode;
            mPlaceClickItemView.setContent(pathInfo.address);
        } else if (resultCode == KeyValue.RESULT_DELIVER) {


            mGoodsInfo.placeprovince = pathInfo.placeprovince;
            mGoodsInfo.placecity = pathInfo.placecity;
            mGoodsInfo.placecounty = pathInfo.placecounty;
            mGoodsInfo.placecitycode = pathInfo.placecitycode;

            mGoodsInfo.deliverprovince = pathInfo.placeprovince;
            mGoodsInfo.delivercity = pathInfo.placecity;
            mGoodsInfo.delivercounty = pathInfo.placecounty;
            mGoodsInfo.delivercitycode = pathInfo.placecitycode;
            mDeliverClickItemView.setContent(pathInfo.address);
        } else if (resultCode == KeyValue.RESULT_DESTINATION) {
            mGoodsInfo.destinationprovince = pathInfo.placeprovince;
            mGoodsInfo.destinationcity = pathInfo.placecity;
            mGoodsInfo.destinationcounty = pathInfo.placecounty;
            mGoodsInfo.destinationcitycode = pathInfo.placecitycode;
            mDestinationClickItemView.setContent(pathInfo.address);
        }
    }

    //设置选择的历史路线信息
    protected void setSelectPathHistory(CitySearchView placeClickItemView,
                                        InputItemView placeAddressInputItemView,
                                        InputItemView placeUserInputItemView,
                                        InputItemView placePhoneInputItemView,
                                        SourcePathInfo sourcePathInfo) {
        if (sourcePathInfo == null) return;
        placeClickItemView.setContent(sourcePathInfo.address);
        placeAddressInputItemView.setContent(sourcePathInfo.placedetails);
        placeUserInputItemView.setContent(sourcePathInfo.placeusername);
        placePhoneInputItemView.setContent(sourcePathInfo.placetel);


    }

    @OnClick(R.id.activity_inquiry_deliver_goods_step_one_nextButton)
    public void next() {


        String placeUserName = mPlaceUserInputItemView.getContent();
        String placeAddress = mPlaceAddressInputItemView.getContent();
        String placePhone = mPlacePhoneInputItemView.getContent();

        String deliverUserName = mDeliverUserInputItemView.getContent();
        String deliverAddress = mDeliverAddressInputItemView.getContent();
        String deliverPhone = mDeliverPhoneInputItemView.getContent();

        String destinationUserName = mDestinationUserInputItemView.getContent();
        String destinationAddress = mDestinationAddressInputItemView.getContent();
        String destinationPhone = mDestinationPhoneInputItemView.getContent();


        String deliverRegion = mDeliverClickItemView.getContent();
        String destinationRegion = mDestinationClickItemView.getContent();

//        mGoodsInfo.placeusername = placeUserName;
//        mGoodsInfo.placedetails = placeAddress;
//        mGoodsInfo.placetel = placePhone;

        mGoodsInfo.placeusername = deliverUserName;
        mGoodsInfo.placedetails = deliverAddress;
        mGoodsInfo.placetel = deliverPhone;

        mGoodsInfo.deliverdetails = deliverAddress;
        mGoodsInfo.deliverusername = deliverUserName;
        mGoodsInfo.delivertel = deliverPhone;

        mGoodsInfo.destinationdetails = destinationAddress;
        mGoodsInfo.destinationusername = destinationUserName;
        mGoodsInfo.destinationtel = destinationPhone;

//        if (TextUtils.isEmpty(mGoodsInfo.placeprovince)) {
//            showToast("取单地不能为空");
//            return;
//        }
//
//        if (TextUtils.isEmpty(mGoodsInfo.placeusername)) {
//            showToast("取单人不能为空");
//            return;
//        }
//        if (TextUtils.isEmpty(mGoodsInfo.placetel)) {
//            showToast("取单人电话不能为空");
//            return;
//        }

        if (TextUtils.isEmpty(deliverRegion)) {
            showToast("装货地不能为空");
            return;
        }
        if (TextUtils.isEmpty(mGoodsInfo.deliverParentCode)) {
            showToast("装货地必须精确到市级以上");
            return;
        }

        if (TextUtils.isEmpty(mGoodsInfo.delivercitycode)) {
            showToast("请在装货地列表中选择地区");
            return;
        }

//        if (TextUtils.isEmpty(mGoodsInfo.deliverusername)) {
//            showToast("装货人不能为空");
//            return;
//        }
//        if (TextUtils.isEmpty(mGoodsInfo.delivertel)) {
//            showToast("装货人电话不能为空");
//            return;
//        }

        if (TextUtils.isEmpty(destinationRegion)) {
            showToast("目的地不能为空");
            return;
        }

        if (TextUtils.isEmpty(mGoodsInfo.destinationParentCode)) {
            showToast("目的地必须精确到市级以上");
            return;
        }
        if (TextUtils.isEmpty(mGoodsInfo.destinationcitycode)) {
            showToast("请在目的地列表中选择地区");
            return;
        }

//        if (TextUtils.isEmpty(mGoodsInfo.destinationusername)) {
//            showToast("收货人不能为空");
//            return;
//        }
//        if (TextUtils.isEmpty(mGoodsInfo.destinationtel)) {
//            showToast("收货人电话不能为空");
//            return;
//        }

        openNextActivity();

    }

    protected void openNextActivity() {
        openNextStepActivity(InquiryDeliverGoodsStepTwoActivity.class);
    }

}
